package com.kssoft.ebd.spider.processor;

import com.kssoft.ebd.spider.common.*;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.HashMap;
import java.util.Map;

/**
 *  通用接口处理器
 * @author liwu
 * @since 2025/6/20
 */
public class AjaxProcessor implements PageProcessor {

    private  Site SITE = Site.me().setRetryTimes(3).setSleepTime(100).setTimeOut(3000);

  private AjaxInterface ajaxInterface;

  private Map<String, String> cookies = new HashMap<>();

  public AjaxProcessor(AjaxInterface ajaxInterface,Site SITE) {
      this.ajaxInterface = ajaxInterface;
      this.SITE = SITE;
  }
    public AjaxProcessor(Map<String, String> cookies,AjaxInterface ajaxInterface) {
        this.ajaxInterface = ajaxInterface;
        this.cookies = cookies;
    }
    @Override
    public void process(Page page) {
        ajaxInterface.ajaxApiResponse(page);
    }

    @Override
    public Site getSite() {
        if(cookies != null && !cookies.isEmpty()) {
            for (Map.Entry<String, String> entry : cookies.entrySet()) {
                SITE.addCookie(entry.getKey(), entry.getValue());
            }
        }

        return SITE;
    }

}
